package zxzx.controller.AcctBsSgmtService.Impl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zxzx.controller.AcctBsSgmtDao.AcctSpecTrstDspnSgmtDao;
import zxzx.controller.AcctBsSgmtService.AcctSpecTrstDspnSgmtService;
import zxzx.controller.pojo.AcctSpecTrstDspnSgmt;
import zxzx.controller.pojo.Constant;
import zxzx.controller.utils.IdWorker;
import zxzx.controller.utils.RandomValue;

@Service
public class AcctSpecTrstDspnSgmtServiceImpl implements AcctSpecTrstDspnSgmtService {

	@Autowired
	private AcctSpecTrstDspnSgmtDao acctSpecTrstDspnSgmtDao;
	@Autowired
	private IdWorker idWorker;
	@Autowired
	private RandomValue randomValue;

	@Override
	public List<AcctSpecTrstDspnSgmt> findAll() {
		return acctSpecTrstDspnSgmtDao.findAll();
	}

	@Override
	public void save(AcctSpecTrstDspnSgmt acctSpecTrstDspnSgmt) {
		
		acctSpecTrstDspnSgmtDao.save(acctSpecTrstDspnSgmt);
	}

	@Override
	public Optional<AcctSpecTrstDspnSgmt> findById(Integer id) {
		return acctSpecTrstDspnSgmtDao.findById(id);
	}

	@Override
	public void update(AcctSpecTrstDspnSgmt acctSpecTrstDspnSgmt) {
		acctSpecTrstDspnSgmtDao.save(acctSpecTrstDspnSgmt);
	}

	@Override
	public void createXml(Integer id) {
		AcctSpecTrstDspnSgmt acctSpecTrstDspnSgmt = acctSpecTrstDspnSgmtDao.findOne(id);
		OutputStreamWriter out = null;
		try {
			String dateFormat = new SimpleDateFormat("yyyyMMdd").format(new Date()).toString();
			String first = randomValue.getEcoIndusCate();
			String tow = "050";
			String th = "520";
			String version = "2.0.0";
			String nextId = idWorker.nextId()+"";
			String substring = nextId.substring(0, 14);
			String randomNum = substring;
			String fileName = first+tow+th+version+"  "+first+randomNum+dateFormat+"0"+"0000001";
			out = new OutputStreamWriter(new FileOutputStream("D:\\xml\\"+fileName+".xml"),"UTF-8");// 写入文件(编码utf-8格式)，文件路径要确认存在
			Document document = createDocument(acctSpecTrstDspnSgmt);
			String asXML = document.getRootElement().asXML();
			out.write(fileName+"\r\n"+asXML);
			// 字符串控制台输出
			OutputFormat format = OutputFormat.createPrettyPrint(); // 转换成字符串
			XMLWriter writer = new XMLWriter(System.out, format);
			writer.write(document);
		} catch (IOException e) {
		} finally {
			if (out != null) {
				try {
					out.flush();
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static Document createDocument(AcctSpecTrstDspnSgmt acctSpecTrstDspnSgmt) {
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement( "Document");
		document.setRootElement(root);
		Element date = root.addElement("AcctSpecTrstDspnSgmt");
		date.addElement(Constant.CagOfTrdNm.toString()).addText(acctSpecTrstDspnSgmt.getCagOfTrdNm().toString());
		Element date2 = date.addElement(Constant.CagOfTrdInf.toString());
		date2.addElement(Constant.ChanTranType.toString()).addText(acctSpecTrstDspnSgmt.getChanTranType());
		date2.addElement(Constant.TranDate.toString()).addText(new SimpleDateFormat("yyyy-MM-dd").format(acctSpecTrstDspnSgmt.getTranDate()));
		date2.addElement(Constant.TranAmt.toString()).addText(acctSpecTrstDspnSgmt.getTranAmt());
		date2.addElement(Constant.DueTranMon.toString()).addText(acctSpecTrstDspnSgmt.getDueTranMon());
		date2.addElement(Constant.DetInfo.toString()).addText(acctSpecTrstDspnSgmt.getDetInfo());
		return document;
	}


}
